﻿using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.Publishing;
using System.Collections.Specialized;
using Inline.SharePoint.ExtendedVariations.Helpers;

namespace Inline.SharePoint.ExtendedVariations.Events
{

    /// <summary>
    /// List Events
    /// </summary>
    public class FieldAddedEventHandler : SPListEventReceiver
    {

       /// <summary>
       /// A field was added.
       /// </summary>
       public override void FieldAdded(SPListEventProperties properties)
       {

           Logger log = new Logger();
           log.WriteEntry(properties.Web.Url, "source", "message", Logger.LogLevel.Informative);

           if (PublishingWeb.IsPublishingWeb(properties.Web))
           {

               PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(properties.Web);
               StringCollection variationUrls = publishingWeb.VariationPublishingWebUrls;

               try
               {

                   FieldHandler fieldHandler = new FieldHandler();
                   fieldHandler.AddField(variationUrls, properties);

               }
               catch (Exception ex)
               {

                   Logger logger = new Logger();
                   logger.WriteEntry(properties.Web.Url,
                       "Inline.SharePoint.ExtendedVariations.Events::FieldAddedEventHandler::FieldAdded",
                       ex.Message + ex.StackTrace,
                       Logger.LogLevel.Error);

               }

           }
           
       }

    }

}
